﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Globalization;

namespace BlueLotus7.Web
{
    public abstract class BaseCalendarViewControl
    {
        protected const string defaultDateFormat = "yyyy-MM-dd";
        protected const string datetimeFormat = "yyyy-MM-dd HH:mm";

        #region Properties

        public CalendarFunctionMode Mode
        {
            get;
            protected set;
        }

        public int Year
        {
            get;
            set;
        }

        public int Month
        {
            get;
            set;
        }

        public DayOfWeek FirstdayOfWeek
        {
            get;
            set;
        }

        public bool UseShortHead
        {
            get;
            set;
        }

        public string BaseUrl
        {
            get;
            set;
        }

        public string BaseRedirectUrl
        {
            get;
            set;
        }

        public string DateFormat
        {
            get;
            set;
        }

        #endregion

        #region Constructor

        public BaseCalendarViewControl(CalendarFunctionMode mode, DayOfWeek firstdayOfWeek, int year, int month)
        {
            this.Mode = mode;
            this.Year = year;
            this.Month = month;
            this.FirstdayOfWeek = firstdayOfWeek;
        }

        public BaseCalendarViewControl(CalendarFunctionMode mode, DayOfWeek firstdayOfWeek, DateTime datetime)
            : this(mode, firstdayOfWeek, datetime.Year, datetime.Month)
        {
        }

        public BaseCalendarViewControl(CalendarFunctionMode mode)
            : this(mode, DayOfWeek.Sunday, DateTime.Now)
        {
        }

        public BaseCalendarViewControl()
            : this(CalendarFunctionMode.Pure)
        {
        }

        #endregion

        #region Public Methods

        public abstract string Render();

        #endregion

        #region protected Methods

        /// <summary>
        /// Generates first day and last day of month
        /// </summary>
        /// <param name="datetime">month date</param>
        /// <param name="firstday">output the first day</param>
        /// <param name="lastday">output the last day</param>
        protected static void GenerateFirstLastDayOfMonth(DateTime datetime, out DateTime firstday, out DateTime lastday)
        {
            firstday = lastday = new DateTime(datetime.Year, datetime.Month, 1);
            lastday = lastday.AddMonths(1).AddSeconds(-1);
        }

        protected static DateTime ReformatTimeValue(DateTime datetime, int min)
        {
            int temp = datetime.Minute % min;
            datetime = datetime.AddMinutes((temp > (int)(min / 2)) ? (min - temp) : -1 * temp);
            return new DateTime(datetime.Year, datetime.Month, datetime.Day, datetime.Hour, datetime.Minute, 0);
        }

        #endregion
    }
}